package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    //RequestMappingHandlerMapping 包含了所有贴有RequestMapping注解的方法
    @Autowired
    private RequestMappingHandlerMapping rmhm;

    //保存和更新员工操作
    public int saveOrUpdate(Permission record) {
        if (record.getId() == null) {
            return permissionMapper.insert(record);
        } else {
            return permissionMapper.updateByPrimaryKey(record);
        }
    }

    //查询单个员工操作
    public Permission selectByPrimaryKey(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }


    //员工列表操作
    public List<Permission> selectAll() {
        return permissionMapper.selectAll();
    }

    /**
     * 分页操作
     * @param qo
     * @return
     */
    public PageResult query(QueryObject qo) {
        int count = permissionMapper.queryCount(qo);
        if (count == 0) {
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<Permission> data = permissionMapper.queryList(qo);
        return new PageResult(count, data);
    }

    /**
     * 权限加载
     */
    public void reload() {
        //查询数据库中所有的权限表达式
        List<String> resource = permissionMapper.selectAllResource();
        //RequestMappingHandlerMapping 管理所有的贴有RequestMapping注解的方法
        Collection<HandlerMethod> methods = rmhm.getHandlerMethods().values();
        //迭代methods所有的方法
        for (HandlerMethod method : methods) {
            //找出贴有RequiresPermissions注解
            RequiresPermissions anno = method.getMethodAnnotation(RequiresPermissions.class);
            //该方法贴有RequiresPermissions注解
            if(anno != null){
                String[] value = anno.value();
                //如果该权限表达式不存在数据库 才能添加进去数据库
                if(!resource.contains(value[0])){
                    Permission p = new Permission();
                    p.setName(value[1]);
                    p.setResource(value[0]);
                    //插入到数据库中
                    permissionMapper.insert(p);
                }
            }
        }
    }

    /**
     * 根据id查询权限信息
     * @param roleId
     * @return
     */
    public List<Permission> selectByRoleId(Long roleId) {
        return permissionMapper.selectByRoleId(roleId);
    }

    /**
     * 根据员工id查询权限信息
     * @param employeeId
     * @return
     */
    public List<String> selectByEmployeeId(Long employeeId) {
        return permissionMapper.selectByEmployeeId(employeeId);
    }
}
